﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Config;
using LTJ.Common;
using LTJ.Common.Generic;
using LTJ.Entity;
using LTJ.FrameWork;

namespace LTJ.WebPage.WebSite
{
    /// <summary>
    /// 查询结果页
    /// </summary>
    public partial class routedatalist : BasePage
    {
        protected int atype = SASRequest.GetInt("acttype", 1);
        protected string keyword = SASRequest.GetString("keyword");
        protected List<RouteBaseInfo> routesTab = new List<RouteBaseInfo>();

        protected int pageindex = SASRequest.GetInt("currentPage", 1);
        protected int countstr = 0;
        protected int pagesize = 15;
        protected string pageList = "";
        protected string pageStr = "";
        protected string parmestr = "";
        /// <summary>
        /// 起运港口
        /// </summary>
        protected string loadport = SASRequest.GetString("loadport");
        /// <summary>
        /// 目的港口
        /// </summary>
        protected string destport = SASRequest.GetString("destport");
        /// <summary>
        /// 航线
        /// </summary>
        protected string routestr = SASRequest.GetString("routestr");
        /// <summary>
        /// 船公司
        /// </summary>
        protected string boatname = SASRequest.GetString("boatname");
        /// <summary>
        /// 航线名称
        /// </summary>
        protected string rname = SASRequest.GetString("rname");
        /// <summary>
        /// 船公司名称
        /// </summary>
        protected string bname = SASRequest.GetString("bname");

        protected override void ShowPage()
        {
            if (pageindex < 0) pageindex = 0;

            string strWhere = RouteBases.GetRouteBaseCondation(0, 2, loadport, destport, routestr, boatname);
            int rtype = 0;
            int objid = -1;

            if (userinfo.gid == Convert.ToInt16(ManageGroup.CorpGroup))
            {
                rtype = 2;
                objid = userinfo.cid;
            }
            else if (userinfo.gid == Convert.ToInt16(ManageGroup.NormalGroup))
            {
                rtype = 1;
                objid = userinfo.id;
                strWhere = "(" + strWhere + " AND route_1.rid > 0) OR (" + strWhere + " AND [View_routebaseinfo_price].cid = " + userinfo.cid + ")";
            }

            countstr = RouteBases.GetRouteBaseListCount(rtype, objid, strWhere);
            routesTab = RouteBases.GetRouteBaseSearchByPage(rtype, objid, userinfo.cid, pageindex, pagesize, strWhere, "", "");
            parmestr = "loadport=" + Utils.UrlEncode(loadport) + "&destport=" + Utils.UrlEncode(destport) + "&routestr=" + routestr + "&boatname=" + boatname + "&acttype=" + atype + "&rname=" + Utils.UrlEncode(rname) + "&bname=" + Utils.UrlEncode(bname);
            pageStr = Utils.SearchPage2(countstr, pagesize, pageindex, "", parmestr);
            pageList = Utils.SearchPageList(countstr, pagesize, pageindex, "", parmestr);

            if (pageindex <= 1 && config.UserVisitLog == 1)
            {
                string lastcommenttime = Utils.GetCookie("lastsearchtime");
                if (lastcommenttime != "")
                {
                    int interval = Utils.StrDateDiffSeconds(lastcommenttime, 5);
                    if (interval < 0)
                    {
                        return;
                    }
                    else
                    {
                        OpLogs.CreateUserVisitWholeSearchLog(userid, userid > 0 ? username : "匿名用户", SASRequest.GetIP(), destport, loadport, routestr, boatname);
                    }
                }
                Utils.WriteCookie("lastsearchtime", System.DateTime.Now.ToString());
            }
        }

        /// <summary>
        /// 价格生成
        /// </summary>
        /// <param name="p1">底价</param>
        /// <param name="p2">港口价</param>
        /// <param name="p3">航线价</param>
        /// <returns></returns>
        protected string GetSeaPrice(string p1, string p2, string p3, string p4, string p5)
        {
            float price1 = TypeConverter.StrToFloat(p1, 0);
            int price2 = TypeConverter.StrToInt(p2, 0);
            int price3 = TypeConverter.StrToInt(p3, 0);
            int price4 = TypeConverter.StrToInt(p4, 0);
            int price5 = TypeConverter.StrToInt(p5, 0);

            if (price2 > 0) return (price1 + price2).ToString();
            else if (price3 > 0) return (price1 + price3).ToString();
            else if (price4 > 0) return (price1 + price4).ToString();
            else if (price5 > 0) return (price1 + price5).ToString();
            else return price1.ToString();
        }

        protected string GetSeaPrce(RouteBaseInfo priceinfo, int type)
        {
            float price1 = 0;

            if (type == 1)
            {
                price1 = TypeConverter.StrToFloat(priceinfo._20GP, 0);
                if (SumPrice(priceinfo._20GP1, priceinfo._40GP1, priceinfo._40HQ1) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP1, 0);
                else if (SumPrice(priceinfo._20GP2, priceinfo._40GP2, priceinfo._40HQ2) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP2, 0);
                else if (SumPrice(priceinfo._20GP5, priceinfo._40GP5, priceinfo._40HQ5) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP5, 0);
                else if (SumPrice(priceinfo._20GP6, priceinfo._40GP6, priceinfo._40HQ6) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP6, 0);
                else if (SumPrice(priceinfo._20GP3, priceinfo._40GP3, priceinfo._40HQ3) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP3, 0);
                else if (SumPrice(priceinfo._20GP4, priceinfo._40GP4, priceinfo._40HQ4) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP4, 0);
                if (priceinfo.cid != companyinfo.id) price1 = price1 + TypeConverter.StrToInt(priceinfo._20GP0, 0);
            }
            else if (type == 2)
            {
                price1 = TypeConverter.StrToFloat(priceinfo._40GP, 0);
                if (SumPrice(priceinfo._20GP1, priceinfo._40GP1, priceinfo._40HQ1) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP1, 0);
                else if (SumPrice(priceinfo._20GP2, priceinfo._40GP2, priceinfo._40HQ2) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP2, 0);
                else if (SumPrice(priceinfo._20GP5, priceinfo._40GP5, priceinfo._40HQ5) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP5, 0);
                else if (SumPrice(priceinfo._20GP6, priceinfo._40GP6, priceinfo._40HQ6) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP6, 0);
                else if (SumPrice(priceinfo._20GP3, priceinfo._40GP3, priceinfo._40HQ3) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP3, 0);
                else if (SumPrice(priceinfo._20GP4, priceinfo._40GP4, priceinfo._40HQ4) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP4, 0);
                if (priceinfo.cid != companyinfo.id) price1 = price1 + TypeConverter.StrToInt(priceinfo._40GP0, 0);
            }
            else if (type == 3)
            {
                price1 = TypeConverter.StrToFloat(priceinfo._40HQ, 0);
                if (SumPrice(priceinfo._20GP1, priceinfo._40GP1, priceinfo._40HQ1) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ1, 0);
                else if (SumPrice(priceinfo._20GP2, priceinfo._40GP2, priceinfo._40HQ2) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ2, 0);
                else if (SumPrice(priceinfo._20GP5, priceinfo._40GP5, priceinfo._40HQ5) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ5, 0);
                else if (SumPrice(priceinfo._20GP6, priceinfo._40GP6, priceinfo._40HQ6) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ6, 0);
                else if (SumPrice(priceinfo._20GP3, priceinfo._40GP3, priceinfo._40HQ3) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ3, 0);
                else if (SumPrice(priceinfo._20GP4, priceinfo._40GP4, priceinfo._40HQ4) > 0) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ4, 0);
                if (priceinfo.cid != companyinfo.id) price1 = price1 + TypeConverter.StrToInt(priceinfo._40HQ0, 0);
            }

            return price1.ToString();
        }

        private int SumPrice(string p1, string p2, string p3)
        {
            int price1 = TypeConverter.StrToInt(p1, 0);
            int price2 = TypeConverter.StrToInt(p2, 0);
            int price3 = TypeConverter.StrToInt(p3, 0);

            return price1 + price2 + price3;
        }

        /// <summary>
        /// 其他价格生成
        /// </summary>
        protected string GetOtherPrice(string price, int ordernum)
        {
            string[] pricearray = price.Split(',');

            if (pricearray.Length < 3) return Utils.StrToInt(price, 0).ToString();
            else return pricearray[ordernum - 1] == "" ? "0" : pricearray[ordernum - 1];
        }
    }
}